﻿using System;
using System.Collections.Generic;
using NailsFramework.Persistence;
using CID.Domain.Operations;

namespace CID.Domain.Infrastructure
{
    public class DeploymentAgent : Model<DeploymentAgent>, CID.Domain.Infrastructure.IDeploymentAgent
    {
        public virtual int Id { get; private set; }
        public virtual string Name { get; set; }
        public virtual bool Enabled { get; set; }
        public virtual string Address { get; set; }

        private IList<ILocation> locations = new List<ILocation>();
        public virtual IEnumerable<ILocation> Locations { get { return locations; } }

        protected DeploymentAgent()
        {
            Enabled = true;
        }

        public DeploymentAgent(string name, string address) : this()
        {
            Name = name;
            Address = address;
        }

        public virtual void AddLocation(ILocation location)
        { 
            if(!locations.Contains(location))
            {
                if(location.DeploymentAgent != null) location.DeploymentAgent.RemoveLocation(location);
                locations.Add(location);
                location.DeploymentAgent = this;
            }
        }

        public virtual void RemoveLocation(ILocation location)
        { 
            if(locations.Contains(location))
            {
                locations.Remove(location);
                location.DeploymentAgent = null;
            }
        }
    }
}
